'use strict';
module.exports = {
	/**
	 * 小程序用户兑换卡密-会员
	 * @url client/user/kh/exchange_cdkey 前端调用的url参数地址
	 */
	main: async (event) => {
		const db = uniCloud.database();
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, _ } = util;
		let { uid, key } = data;
		let res = { code: 0, msg: "1", keyinfo: "" };
		// 业务逻辑开始-----------------------------------------------------------
		// 1天 = 86400000毫秒
		// 1月 = 2592000000毫秒
		// 1年 = 31536000000毫秒
		var nowtime = new Date().getTime()

		res.keyinfo = await db.collection('a_cdkey').doc(key).get()
		if (res.keyinfo.data.length == 0) {
			res.tip = "卡密不存在，请核实后重试";
			return res;
		}

		//卡密状态不为0，则不允许核销
		if (res.keyinfo.data[0].state != 0) {
			res.tip = "卡密状态错误，请检查";
			//1代表卡密已核销
			if (res.keyinfo.data[0].state == 1) {
				res.tip = "卡密已核销，请勿重复核销";
			}
			return res;
		}

		let keys = res.keyinfo.data[0];
		
		let add_longtime = 0; //卡密可兑换新增的时长
		let add_jindou = 0; //卡密可兑换新增的金豆数
		
		//卡密类型为金豆卡
		if (keys.type == 4) {
			add_jindou = keys.bean
			// let updateJindou = await db.collection('uni-id-users').doc(userInfo._id).update({
			// 	balance: _.inc(add_jindou)
			// })
			let updateJindou = await vk.baseDao.updateAndReturn({
				dbName: "uni-id-users",
				whereJson: {
					_id: userInfo._id
				},
				dataJson: {
					balance: _.inc(add_jindou)
				}
			});
			//添加金豆收支明细
			let add_jindou1 = await db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname || "未绑定",
				change_num: add_jindou, //增加的金豆数量
				changed_num: updateJindou.balance, //支付后金豆余额
				change_reason: "卡密:" + key + "兑换" + add_jindou + "金豆",
				type: "income", //类型：expend:支出,income:收入。
				appid: userInfo.appid || "",
				appname: userInfo.appname || "",
				_add_time: nowtime,
				_add_time_str: vk.pubfn.timeFormat(nowtime, "yyyy-MM-dd hh:mm:ss")
			})
			//更新a_cdkey表的兑换信息
			let updateCdkey = await db.collection('a_cdkey').doc(key).update({
				state: 1, //卡密兑换状态
				user_id: userInfo._id, //卡密兑换用户ID
				user_nickname: userInfo.nickname, //卡密兑换用户昵称
				used_time: nowtime //卡密兑换时间
			})
			return res;
		}
		//卡密类型为天卡
		if (keys.type == 1) {
			add_longtime = 86400000 * keys.number
		}
		//卡密类型为月卡
		if (keys.type == 2) {
			add_longtime = 2592000000 * keys.number
		}
		//卡密类型为年卡
		if (keys.type == 3) {
			add_longtime = 31536000000 * keys.number
		}


		let new_plus_expire_time = 0; //新的会员到期时间
		//如果该用户已经是会员，就在会员到期时间基础上加新增时长
		if (userInfo.plus_type == 1) {
			new_plus_expire_time = userInfo.plus_expire_time + add_longtime
		}

		//如果该用户还不是会员，就在当前时间基础上加新增时长
		if (userInfo.plus_type == 0) {
			new_plus_expire_time = nowtime + add_longtime
		}

		let updateVip = await db.collection('uni-id-users').doc(userInfo._id).update({
			plus_type: 1,
			plus_expire_time: new_plus_expire_time
		})


		//更新a_cdkey表的兑换信息
		let updateCdkey = await db.collection('a_cdkey').doc(key).update({
			state: 1, //卡密兑换状态
			user_id: userInfo._id, //卡密兑换用户ID
			user_nickname: userInfo.nickname, //卡密兑换用户昵称
			used_time: nowtime //卡密兑换时间
		})




		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
